{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Count Complete Subarrays in an Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #sliding-window"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #滑动窗口"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: countCompleteSubarrays"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #统计完全子数组的数目"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个由 <strong>正</strong> 整数组成的数组 <code>nums</code> 。</p>\n",
    "\n",
    "<p>如果数组中的某个子数组满足下述条件，则称之为 <strong>完全子数组</strong> ：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>子数组中 <strong>不同</strong> 元素的数目等于整个数组不同元素的数目。</li>\n",
    "</ul>\n",
    "\n",
    "<p>返回数组中 <strong>完全子数组</strong> 的数目。</p>\n",
    "\n",
    "<p><strong>子数组</strong> 是数组中的一个连续非空序列。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [1,3,1,2,2]\n",
    "<strong>输出：</strong>4\n",
    "<strong>解释：</strong>完全子数组有：[1,3,1,2]、[1,3,1,2,2]、[3,1,2] 和 [3,1,2,2] 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [5,5,5,5]\n",
    "<strong>输出：</strong>10\n",
    "<strong>解释：</strong>数组仅由整数 5 组成，所以任意子数组都满足完全子数组的条件。子数组的总数为 10 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 1000</code></li>\n",
    "\t<li><code>1 &lt;= nums[i] &lt;= 2000</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [count-complete-subarrays-in-an-array](https://leetcode.cn/problems/count-complete-subarrays-in-an-array/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [count-complete-subarrays-in-an-array](https://leetcode.cn/problems/count-complete-subarrays-in-an-array/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,3,1,2,2]', '[5,5,5,5]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        m = len(set(nums))\n",
    "        cnt = Counter()\n",
    "        ans = left = 0\n",
    "        for v in nums:  # 枚举子数组右端点 v=nums[i]\n",
    "            cnt[v] += 1\n",
    "            while len(cnt) == m:\n",
    "                x = nums[left]\n",
    "                cnt[x] -= 1\n",
    "                if cnt[x] == 0:\n",
    "                    del cnt[x]\n",
    "                left += 1\n",
    "            ans += left  # 子数组左端点 < left 的都是合法的\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        total_cnt = len(set(nums))\n",
    "        cnt = Counter()\n",
    "        ans = left = 0\n",
    "        for right, num in enumerate(nums):\n",
    "            cnt[num] += 1\n",
    "            while len(cnt) == total_cnt:\n",
    "                left_value = nums[left]\n",
    "                cnt[left_value] -= 1\n",
    "                if cnt[left_value] == 0:\n",
    "                    cnt.pop(left_value)\n",
    "                left += 1\n",
    "            ans += left\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        d = len(set(nums))\n",
    "        res = 0\n",
    "        \n",
    "        for i in range(n):\n",
    "            cnt = Counter()\n",
    "            for j in range(i, n):\n",
    "                cnt[nums[j]] += 1\n",
    "                if len(cnt) == d:\n",
    "                    res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        m=len(set(nums))\n",
    "        n=len(nums)\n",
    "        ans=0\n",
    "        for i in range(n):\n",
    "            s=set()\n",
    "            for j in range(i,n):\n",
    "                s.add(nums[j])\n",
    "                if len(s)==m:\n",
    "                    ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        m = len(set(nums))\n",
    "        cnt = Counter()\n",
    "        ans = left = 0\n",
    "        for r in nums:\n",
    "            cnt[r] += 1\n",
    "            while len(cnt) == m:\n",
    "                x = nums[left]\n",
    "                cnt[x] -= 1\n",
    "                if cnt[x] == 0:\n",
    "                    del cnt[x]\n",
    "                left += 1\n",
    "            ans += left\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        m = len(set(nums))\n",
    "        cnt = Counter()\n",
    "        ans = left = 0\n",
    "        for v in nums:\n",
    "            cnt[v] += 1\n",
    "            while len(cnt) == m:\n",
    "                x = nums[left]\n",
    "                cnt[x] -= 1\n",
    "                if cnt[x] == 0:\n",
    "                    del cnt[x]\n",
    "                left += 1\n",
    "            ans += left\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        res = 0\n",
    "        left,right = 0,0\n",
    "        window = {}\n",
    "        need = len(set(nums))\n",
    "        while right < n:\n",
    "            window[nums[right]] = window.get(nums[right],0) + 1\n",
    "            while len(window) == need:\n",
    "                # 当当前window满足的时候，再加上后面的部分也都满足\n",
    "                # 比如5 满足，则[5,5],[5,5,5],[5,5,5,5]都满足\n",
    "                res += 1 + n - 1 - right  # 1为当前的子数组，n-1-right为后面的子数组数量\n",
    "                window[nums[left]] -= 1\n",
    "                if window[nums[left]] == 0:\n",
    "                    window.pop(nums[left])\n",
    "                left += 1\n",
    "            right += 1\n",
    "        return res\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        n = len(set(nums))\n",
    "        cnt = Counter()\n",
    "        ans, l = 0, 0\n",
    "        for x in nums:\n",
    "            cnt[x] += 1\n",
    "            while len(cnt) == n:\n",
    "                x = nums[l]\n",
    "                cnt[x] -= 1\n",
    "                if cnt[x] == 0: del cnt[x]\n",
    "                l += 1\n",
    "            ans += l\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        t = len(set(nums))\n",
    "        cnt = Counter()\n",
    "        l = ans = 0\n",
    "        for r, x in enumerate(nums):\n",
    "            cnt[x] += 1\n",
    "            while len(cnt) == t:\n",
    "                y = nums[l]\n",
    "                cnt[y] -= 1\n",
    "                if cnt[y] == 0:\n",
    "                    del cnt[y]\n",
    "                l += 1\n",
    "            ans += l\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        m = len(set(nums))\n",
    "        def aaa(k):\n",
    "            N = len(nums)\n",
    "            left, right = 0, 0\n",
    "            ans = 0\n",
    "            cnt = Counter()\n",
    "            for right, x in enumerate(nums):\n",
    "                cnt[x] += 1\n",
    "                while len(cnt) > k:\n",
    "                    cnt[nums[left]] -= 1\n",
    "                    if cnt[nums[left]] == 0:\n",
    "                        del cnt[nums[left]]\n",
    "                    left += 1\n",
    "                ans += right - left + 1\n",
    "            return ans\n",
    "        return aaa(m) - aaa(m - 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        # target = len(set(nums))\n",
    "        # cnt = Counter()\n",
    "        # ans = left = 0\n",
    "        # for v in nums:\n",
    "        #     cnt[v] += 1\n",
    "        #     while len(cnt) == target:\n",
    "        #         x = nums[left]\n",
    "        #         cnt[x] -= 1\n",
    "        #         if cnt[x] == 0:\n",
    "        #             del cnt[x]\n",
    "        #         left += 1\n",
    "        #     ans += left\n",
    "        # return ans\n",
    "\n",
    "        n = len(nums)\n",
    "        target = len(set(nums))\n",
    "        st,end = 0,0\n",
    "        ans = 0\n",
    "        while end < n:\n",
    "            if len(set(nums[st:end+1])) == target:\n",
    "                ans += n - end\n",
    "                st += 1\n",
    "            else:\n",
    "                end += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        nums_set = list(set(nums))\n",
    "        nums_len = len(nums_set)\n",
    "\n",
    "        i = 0\n",
    "        j = nums_len\n",
    "        count = 0\n",
    "\n",
    "        for i in range(n - nums_len + 1):\n",
    "            j = i + nums_len\n",
    "            while len(set(nums[i:j])) < nums_len and j < n + 1:\n",
    "                j += 1\n",
    "            count = count + n - j + 1\n",
    "        \n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        min_num = min(nums)\n",
    "        hash_list = [0] * (max(nums) - min(nums) + 1)\n",
    "        for num in nums:\n",
    "            hash_list[num - min_num] += 1\n",
    "        l, r = 0, len(nums) - 1\n",
    "        count = 0\n",
    "        while r>=l:\n",
    "            if hash_list[nums[r]-min_num] > 1:\n",
    "                hash_list[nums[r]-min_num] -= 1\n",
    "                r -= 1\n",
    "            else:\n",
    "                break\n",
    "        count += len(nums) - r\n",
    "        # print(hash_list[nums[1] - min_num])\n",
    "        while l <= len(nums) - 1:\n",
    "            if(hash_list[nums[l] - min_num] > 1):\n",
    "                if(r == l):\n",
    "                    r += 1\n",
    "                hash_list[nums[l] - min_num] -= 1\n",
    "                l += 1\n",
    "                count += len(nums) - r\n",
    "     \n",
    "            else: #hash_list[num[l] - min_num] == 1\n",
    "                if(r == l):\n",
    "                    r += 1\n",
    "                while(r <= len(nums)-1 and nums[r] != nums[l]):\n",
    "                    r += 1\n",
    "                    if(r == len(nums)):\n",
    "                        return count\n",
    "                    hash_list[nums[r] - min_num] += 1\n",
    "\n",
    "                count += len(nums) - r\n",
    "                hash_list[nums[l] - min_num] -= 1\n",
    "                l += 1\n",
    "        return count        \n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        assert n >= 1\n",
    "        nn=len(set(nums))\n",
    "        dd = Counter()\n",
    "        for i, v in enumerate(nums):\n",
    "            dd[v]+=1\n",
    "            if len(dd) == nn:\n",
    "                break\n",
    "        le, ri = 0, i\n",
    "        ans = n-i\n",
    "        # print(le, ri, ans)\n",
    "        while True:\n",
    "            dd[nums[le]] -= 1\n",
    "            if dd[nums[le]] == 0:\n",
    "                del dd[nums[le]]\n",
    "            le += 1\n",
    "            # print(dd)\n",
    "            \n",
    "            while len(dd) < nn and ri + 1 < n:\n",
    "                ri += 1\n",
    "                dd[nums[ri]] += 1\n",
    "            if len(dd) == nn:\n",
    "                ans += n-ri\n",
    "                # print(le, ri, ans)\n",
    "            else:\n",
    "                break\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums):\n",
    "        n = len(nums)\n",
    "        diffElement = len(set(nums))\n",
    "        wdiffElement = 0\n",
    "        whashTable = {}\n",
    "        left = right = 0\n",
    "        count = 0\n",
    "        while right < n:\n",
    "            addE = nums[right]\n",
    "            num = whashTable.get(addE, 0)\n",
    "            if num == 0:\n",
    "                wdiffElement += 1\n",
    "            whashTable[addE] = num + 1\n",
    "            right += 1\n",
    "            while wdiffElement == diffElement:\n",
    "                # 当此时已经是“完全子数组”,那么后面再加元素也是“完全子数组”。\n",
    "                # 举例：[3,1,2]是完全子数组,那么[3,1,2,2]也是。\n",
    "                count += n - right + 1\n",
    "                removeE = nums[left]\n",
    "                # 获取此时removeE的数量，如果此时数量为1那么之后它将变为0\n",
    "                # 即此时就要wdiffElement--, 代表窗口中少了一个不同元素的数目\n",
    "                num = whashTable[removeE]\n",
    "                if num == 1:\n",
    "                    wdiffElement -= 1\n",
    "                whashTable[removeE] = num - 1\n",
    "                left += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        m = len(set(nums))\n",
    "        ans = 0\n",
    "        c = Counter()\n",
    "        left = 0\n",
    "        for x in nums:\n",
    "            c[x] += 1\n",
    "            while len(c) == m:\n",
    "                y = nums[left]\n",
    "                c[y] -= 1\n",
    "                if c[y] == 0:\n",
    "                    del c[y]\n",
    "                left += 1\n",
    "            ans += left\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        num,n=len(Counter(nums)),len(nums)\n",
    "        t=Counter()\n",
    "        i,l,res=0,0,0\n",
    "        while i < n:\n",
    "            t[nums[i]] += 1\n",
    "            while len(t) != num:\n",
    "                    i += 1\n",
    "                    t[nums[i]]+=1\n",
    "            while t[nums[l]]>1:\n",
    "                t[nums[l]]-=1\n",
    "                l+=1\n",
    "            i+=1\n",
    "            res+=l+1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        m = len(set(nums))\n",
    "        cnt = Counter()\n",
    "        ans = left = 0\n",
    "        for right in nums:  # 枚举子数组右端点 v=nums[i]\n",
    "            ans += left  # 子数组左端点 < left 的都是合法的\n",
    "            cnt[right] += 1\n",
    "            while len(cnt) == m:\n",
    "                ans += 1  # 子数组左端点等于 left 是合法的\n",
    "                x = nums[left]\n",
    "                cnt[x] -= 1\n",
    "                if cnt[x] == 0:\n",
    "                    del cnt[x]\n",
    "                left += 1\n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        m=len(set(nums))\n",
    "        n=len(nums)\n",
    "        # left=0\n",
    "        ans=0\n",
    "        for i in range(n):\n",
    "            s=set()\n",
    "            # 左端点\n",
    "            # s.add(i)\n",
    "            for j in range(i,n):\n",
    "                # 右端点\n",
    "                s.add(nums[j])\n",
    "                if len(s)==m:\n",
    "                    ans+=1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        m = len(set(nums))\n",
    "        cnt = Counter()\n",
    "        ans = left = 0\n",
    "        for v in nums:  # 枚举子数组右端点 v=nums[i]\n",
    "            cnt[v] += 1\n",
    "            while len(cnt) == m:\n",
    "                x = nums[left]\n",
    "                cnt[x] -= 1\n",
    "                if cnt[x] == 0:\n",
    "                    del cnt[x]\n",
    "                left += 1\n",
    "            ans += left  # 子数组左端点 < left 的都是合法的\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        target = len(set(nums))\n",
    "        cnt = Counter()\n",
    "        ans = left = 0\n",
    "        for v in nums:\n",
    "            cnt[v] += 1\n",
    "            while len(cnt) == target:\n",
    "                x = nums[left]\n",
    "                cnt[x] -= 1\n",
    "                if cnt[x] == 0:\n",
    "                    del cnt[x]\n",
    "                left += 1\n",
    "            ans += left\n",
    "        return ans\n",
    "        # n = len(nums)\n",
    "        # target = len(Counter(nums).keys())\n",
    "        # st,end = 0,0\n",
    "        # ans = 0\n",
    "        # while end < n:\n",
    "        #     if len(Counter(nums[st:end+1]).keys()) == target:\n",
    "        #         ans += n - end\n",
    "        #         st += 1\n",
    "        #     else:\n",
    "        #         end += 1\n",
    "        # return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        map = {}\n",
    "        for num in nums:\n",
    "            map[num] = map.get(num, 0) + 1\n",
    "        count = len(map)\n",
    "        ans = 0\n",
    "        d1 = collections.defaultdict(int)\n",
    "        r = 0\n",
    "        for l in range(n):\n",
    "            while r < n and len(d1) < count:\n",
    "                d1[nums[r]] += 1\n",
    "                r += 1\n",
    "            if len(d1) < count:\n",
    "                break\n",
    "            ans += n-r+1\n",
    "            # print(ans, r, d1)\n",
    "            d1[nums[l]] -= 1\n",
    "            if d1[nums[l]] == 0:\n",
    "                del d1[nums[l]]\n",
    "        return ans\n",
    "           \n",
    "                \n",
    "                \n",
    "\n",
    "\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        # count=len(set(nums))\n",
    "        # res=0\n",
    "        # for j in range(len(nums)):\n",
    "        #     temp=set()\n",
    "        #     for i in nums[j:]:\n",
    "        #         temp.add(i)\n",
    "        #         if len(temp)==count:\n",
    "        #             res+=1\n",
    "        # return res\n",
    "        n=len(set(nums))\n",
    "        temp=Counter()\n",
    "        res=left=0\n",
    "        for x in nums:\n",
    "            temp[x]+=1\n",
    "            while len(temp)==n:\n",
    "                y=nums[left]\n",
    "                temp[y]-=1\n",
    "                if temp[y]==0:\n",
    "                    del temp[y]\n",
    "                left+=1\n",
    "            res+=left\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        l=0\n",
    "        r=0\n",
    "        n=len(nums)\n",
    "        q=Counter()\n",
    "        f=Counter(nums)\n",
    "        ans=0\n",
    "        while r<n:\n",
    "          q[nums[r]]+=1\n",
    "          while len(q)>=len(f):\n",
    "             q[nums[l]]-=1\n",
    "             if q[nums[l]]==0:\n",
    "                del q[nums[l]]\n",
    "             \n",
    "             l+=1\n",
    "          ans+=l   \n",
    "          r+=1\n",
    "        return ans          \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        m = len(set(nums))\n",
    "        cnt = Counter()\n",
    "        ans = left = 0\n",
    "        for v in nums:  # 枚举子数组右端点 v=nums[i]\n",
    "            cnt[v] += 1\n",
    "            while len(cnt) == m:\n",
    "                x = nums[left]\n",
    "                cnt[x] -= 1\n",
    "                if cnt[x] == 0:\n",
    "                    del cnt[x]\n",
    "                left += 1\n",
    "            ans += left  # 子数组左端点 < left 的都是合法的\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # # 滑动窗口\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        m = len(set(nums))\n",
    "        cnt = Counter()\n",
    "        ans = left = 0\n",
    "        for v in nums:  # 枚举子数组右端点 v=nums[i]\n",
    "            cnt[v] += 1\n",
    "            while len(cnt) == m:\n",
    "                x = nums[left]\n",
    "                cnt[x] -= 1\n",
    "                if cnt[x] == 0:\n",
    "                    del cnt[x]\n",
    "\n",
    "                left += 1\n",
    "\n",
    "            ans += left  # 子数组左端点 < left 的都是合法的\n",
    "\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        map1=defaultdict(int)\n",
    "        cnt=0\n",
    "        for i in range(len(nums)):\n",
    "            if map1[nums[i]]==0:\n",
    "                cnt+=1\n",
    "            map1[nums[i]]+=1\n",
    "        res=0\n",
    "        for i in range(len(nums)):\n",
    "            map1=defaultdict(int)\n",
    "            cnt2=0\n",
    "            for j in range(i,len(nums)):\n",
    "                if map1[nums[j]]==0:\n",
    "                    cnt2+=1\n",
    "                map1[nums[j]]+=1\n",
    "                if cnt2==cnt:\n",
    "                    res+=1\n",
    "                if cnt2>cnt:\n",
    "                    break\n",
    "                \n",
    "        return res\n",
    "                \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        O = len(set(nums))\n",
    "        N = len(nums)\n",
    "        l = 0\n",
    "        counter = Counter()\n",
    "        for num in nums:\n",
    "            counter[num] += 1\n",
    "            while len(counter) == O:\n",
    "                lv = nums[l]\n",
    "                counter[lv] -= 1\n",
    "                if counter[lv] == 0:\n",
    "                    del counter[lv]\n",
    "                l += 1\n",
    "            ans += l\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        m = len(set(nums))\n",
    "        cnt = Counter()\n",
    "        res = 0\n",
    "        left = 0\n",
    "        n = len(nums)\n",
    "        for right in range(n):\n",
    "            cnt[nums[right]]+=1\n",
    "            while len(cnt)>=m:\n",
    "                x = nums[left]\n",
    "                cnt[x]-=1\n",
    "                if cnt[x]==0:\n",
    "                    del cnt[x]\n",
    "                left += 1\n",
    "            res += left if len(cnt)==m-1 else 0\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        C = len(Counter(nums))\n",
    "        print(C)\n",
    "        cnt = Counter()\n",
    "        res = l = 0\n",
    "        for i in range(len(nums)):\n",
    "            cnt[nums[i]] += 1\n",
    "            while len(cnt) == C:\n",
    "                cnt[nums[l]] -= 1\n",
    "                if cnt[nums[l]] == 0:\n",
    "                    del cnt[nums[l]]\n",
    "                l += 1\n",
    "            res += l\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        l, sl = len(nums), len(set(nums))\n",
    "        record = Counter()\n",
    "        left = 0\n",
    "        last = -1\n",
    "        res = 0\n",
    "        for index, i in enumerate(nums):\n",
    "            record[i] += 1\n",
    "            if len(record) == sl:\n",
    "                while(record[nums[left]] > 1):\n",
    "                    record[nums[left]] -= 1\n",
    "                    left += 1\n",
    "                res += (left - last) * (l - index)\n",
    "                last = left\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        k = len(set(tuple(nums)))\n",
    "        ans = 0\n",
    "        l = 0\n",
    "        for r in range(n):\n",
    "            st = tuple(nums[l:r+1])\n",
    "            while len(set(st)) == k:\n",
    "                ans += n-r \n",
    "                l += 1\n",
    "                st = tuple(nums[l:r+1])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import deque\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        # 偏暴力解决方法\n",
    "        # n = len(nums)\n",
    "        # diffs = set(nums)\n",
    "        # res = 0\n",
    "        \n",
    "        # for L in range(0, n):\n",
    "        #     sub_set = set()\n",
    "        #     for R in range(L, n):\n",
    "        #         sub_set.add(nums[R])\n",
    "\n",
    "        #         if sub_set == diffs:\n",
    "        #             res += n - R\n",
    "        #             break            \n",
    "\n",
    "        # return res\n",
    "\n",
    "        # 滑动窗口\n",
    "        n = len(nums)\n",
    "        diffs = len(set(nums))\n",
    "        table = {}\n",
    "        res = 0\n",
    "        R = 0\n",
    "        for L in range(0, n):\n",
    "            if len(table) == diffs:\n",
    "                res += n - R + 1\n",
    "            else:\n",
    "                while R < n:\n",
    "                    if nums[R] not in table:\n",
    "                        table[nums[R]] = 1\n",
    "                    else:\n",
    "                        table[nums[R]] += 1\n",
    "\n",
    "                    if len(table) == diffs:\n",
    "                        res += n - R\n",
    "                        R += 1\n",
    "                        break\n",
    "                    \n",
    "                    R += 1\n",
    "\n",
    "            table[nums[L]] -= 1\n",
    "            if table[nums[L]] == 0:\n",
    "                del table[nums[L]]            \n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, a: List[int]) -> int:\n",
    "        s=set(a)\n",
    "        n=len(a)\n",
    "        ans=0\n",
    "        for i in range(n):\n",
    "            t=set()\n",
    "            for j in range(i,n):\n",
    "                t.add(a[j])\n",
    "                if len(t)==len(s):\n",
    "                    ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        c=Counter()\n",
    "        left=0\n",
    "        n=len(nums)\n",
    "        right=0\n",
    "        w=len(list(set(nums)))\n",
    "        ans=0\n",
    "        while right<n:\n",
    "            c[nums[right]]+=1\n",
    "            right+=1\n",
    "            while len(c.keys())==w:\n",
    "                ans+=n+1-right\n",
    "                c[nums[left]]-=1\n",
    "                if c[nums[left]]==0:\n",
    "                    del c[nums[left]]\n",
    "                left+=1\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        distinct_num_cnt = len(set(nums))\n",
    "        ans = 0\n",
    "        crt_set = set()\n",
    "        for i, num in enumerate(nums):\n",
    "            crt_set.clear()\n",
    "            for j in range(i, n):\n",
    "                crt_set.add(nums[j])\n",
    "                if len(crt_set) == distinct_num_cnt:\n",
    "                    ans += n - j\n",
    "                    break\n",
    "        return ans\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        \"\"\"滑动窗口解决完全子数组问题\"\"\"\n",
    "        m = len(set(nums))\n",
    "        cnt = Counter()\n",
    "        ans = left =0\n",
    "        for v in nums:\n",
    "            cnt[v] +=1\n",
    "            while len(cnt) == m:\n",
    "                x = nums[left]\n",
    "                cnt[x] -= 1\n",
    "                if cnt[x] ==0:\n",
    "                    del cnt[x]\n",
    "                left += 1\n",
    "            ans += left\n",
    "        return ans\n",
    "\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums) -> int:\n",
    "        counter = Counter(nums)\n",
    "        ans = 0\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            cur_cnt = counter.copy()\n",
    "            for j in range(n - 1,i - 1,-1):\n",
    "                ans += 1\n",
    "                cur_cnt[nums[j]] -= 1\n",
    "                if cur_cnt[nums[j]] == 0:\n",
    "                    break\n",
    "            counter[nums[i]] -= 1\n",
    "            if counter[nums[i]] == 0:\n",
    "                break\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        m = len(set(nums))\n",
    "        cnt = Counter()\n",
    "        ans = left = 0\n",
    "        for v in nums:  # 枚举子数组右端点 v=nums[i]\n",
    "            cnt[v] += 1\n",
    "            while len(cnt) == m:\n",
    "                x = nums[left]\n",
    "                cnt[x] -= 1\n",
    "                if cnt[x] == 0:\n",
    "                    del cnt[x]\n",
    "                left += 1\n",
    "            ans += left  # 子数组左端点 < left 的都是合法的\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        m = len(set(nums))\n",
    "        counter = collections.Counter()\n",
    "        l = 0\n",
    "        res = 0\n",
    "        for r in range(len(nums)):\n",
    "            counter[nums[r]] += 1 \n",
    "            while l <= r and len(counter) == m:\n",
    "                counter[nums[l]] -= 1 \n",
    "                if counter[nums[l]] == 0:\n",
    "                    del counter[nums[l]]\n",
    "                l += 1 \n",
    "            res += l\n",
    "        return res \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        target = len(Counter(nums).keys())\n",
    "        st,end = 0,0\n",
    "        ans = 0\n",
    "        while end < n:\n",
    "            if len(Counter(nums[st:end+1]).keys()) == target:\n",
    "                ans += n - end\n",
    "                st += 1\n",
    "            else:\n",
    "                end += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        # count=len(set(nums))\n",
    "        # res=0\n",
    "        # for j in range(len(nums)):\n",
    "        #     temp=set()\n",
    "        #     for i in nums[j:]:\n",
    "        #         temp.add(i)\n",
    "        #         if len(temp)==count:\n",
    "        #             res+=1\n",
    "        # return res\n",
    "        n=len(set(nums))\n",
    "        res=0\n",
    "        for i in range(len(nums)):\n",
    "            temp=set()\n",
    "            for j in nums[i:]:\n",
    "                temp.add(j)\n",
    "                if len(temp)==n:\n",
    "                    res+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        m = len(set(nums))\n",
    "        counter = collections.Counter()\n",
    "        res = 0\n",
    "        l = 0\n",
    "        for r in range(n):\n",
    "            counter[nums[r]] += 1 \n",
    "            while l <= r and len(counter) == m:\n",
    "                counter[nums[l]] -= 1 \n",
    "                if counter[nums[l]] == 0:\n",
    "                    del counter[nums[l]]\n",
    "                l += 1\n",
    "            res += l \n",
    "        return res \n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        l = r = ans = 0\n",
    "        t = len(set(nums))\n",
    "        g = dict()\n",
    "        while r < n:\n",
    "            g[nums[r]] = g.get(nums[r], 0) + 1       \n",
    "            while len(g.keys()) == t:\n",
    "                ans += n - r\n",
    "                v = g[nums[l]]\n",
    "                if v == 1:\n",
    "                    g.pop(nums[l])\n",
    "                else:\n",
    "                    g[nums[l]] = v - 1\n",
    "                l += 1\n",
    "            r += 1\n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        c=Counter(nums)\n",
    "        num,n=len(c.items()),len(nums)\n",
    "        res=0\n",
    "        for i in range(n):\n",
    "            t=Counter()\n",
    "            for j in range(i,n):\n",
    "                t[nums[j]]+=1\n",
    "                if len(t.items())==num:\n",
    "                    res+=n-j\n",
    "                    break\n",
    "        return res                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        left = ans = 0\n",
    "        cnt = defaultdict(int)\n",
    "        n = len(set(nums))\n",
    "        for right, x in enumerate(nums):\n",
    "            cnt[x] += 1\n",
    "            while len(cnt) == n:\n",
    "                cnt[nums[left]] -= 1\n",
    "                if cnt[nums[left]] == 0:\n",
    "                    del cnt[nums[left]]\n",
    "                left += 1\n",
    "            # 加上前面可要可不要的部分\n",
    "            ans += left\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "\n",
    "        # 1,2,1,2,3,2,3,1\n",
    "\n",
    "        d = defaultdict(int)\n",
    "\n",
    "        n = len(set(nums))\n",
    "        l = 0\n",
    "        ret = 0\n",
    "        for r, v in enumerate(nums):\n",
    "\n",
    "            d[v] += 1\n",
    "\n",
    "            if len(d) == n:\n",
    "                while True:\n",
    "                    v = nums[l] \n",
    "\n",
    "                    if d[v] == 1:\n",
    "                        break\n",
    "                    else:\n",
    "                        d[v] -= 1\n",
    "                        l += 1\n",
    "            \n",
    "                ret += l + 1\n",
    "        \n",
    "        return ret\n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        unrepeatable_elements = len(set(nums))\n",
    "        area_dict = {}\n",
    "        count = 0\n",
    "        left = 0\n",
    "        for right, v in enumerate(nums):\n",
    "            if v not in area_dict:\n",
    "                area_dict.update({v: 1})\n",
    "            else:\n",
    "                area_dict[v] += 1\n",
    "            while len(area_dict) == unrepeatable_elements:\n",
    "                count += len(nums) - right\n",
    "                area_dict[nums[left]] -= 1\n",
    "                if area_dict[nums[left]] == 0:\n",
    "                    area_dict.pop(nums[left])\n",
    "                left += 1\n",
    "\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        m = len(set(nums))  # 所有独特元素的数量\n",
    "        cnt = Counter()  # 用于跟踪当前窗口中每个元素的数量\n",
    "        ans = 0  # 完全子数组的数量\n",
    "        left = 0  # 左指针\n",
    "        \n",
    "        for v in nums:  # 枚举子数组右端点 v=nums[i]\n",
    "            cnt[v] += 1  # 更新计数\n",
    "            while len(cnt) == m:  # 当前窗口是一个“完全子数组”\n",
    "                x = nums[left]  # 窗口左端点的元素\n",
    "                cnt[x] -= 1  # 减少计数\n",
    "                if cnt[x] == 0:  # 如果该元素计数变为0，从 Counter 中删除\n",
    "                    del cnt[x]\n",
    "                left += 1  # 移动左指针\n",
    "            ans += left  # 子数组左端点 < left 的都是合法的\n",
    "            \n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        m = len(set(nums))\n",
    "        cnt = Counter()\n",
    "        ans = left = 0\n",
    "        for i in range(len(nums)):\n",
    "            cnt[nums[i]] += 1\n",
    "            while len(cnt) == m:\n",
    "                x = nums[left]\n",
    "                cnt[x] -= 1\n",
    "                if cnt[x] == 0:\n",
    "                    del cnt[x]\n",
    "                left += 1\n",
    "            ans += left\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        rec = set()\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            rec.add(nums[i])\n",
    "        \n",
    "        res = 0\n",
    "        for i in range(n):\n",
    "            tem_s = set()\n",
    "            for j in range(i, n):\n",
    "                tem_s.add(nums[j])\n",
    "                if len(tem_s) == len(rec):\n",
    "                    break\n",
    "            if len(tem_s) == len(rec):\n",
    "                res += (n-j)\n",
    "    \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        m = len(set(nums))\n",
    "        n = len(nums)\n",
    "        counter = collections.Counter()\n",
    "        l = 0\n",
    "        res = 0\n",
    "        for r in range(n):\n",
    "            counter[nums[r]] += 1 \n",
    "            while l <= r and len(counter) == m:\n",
    "                counter[nums[l]] -= 1 \n",
    "                if counter[nums[l]] == 0:\n",
    "                    del counter[nums[l]]\n",
    "                l += 1 \n",
    "            res += l \n",
    "        return res "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        left = ans = 0\n",
    "        cnt = defaultdict(int)\n",
    "        n = len(Counter(nums))\n",
    "        for right, x in enumerate(nums):\n",
    "            cnt[x] += 1\n",
    "            # 加上前面可要可不要的部分\n",
    "            ans += left\n",
    "            while len(cnt) == n:\n",
    "                ans += 1\n",
    "                cnt[nums[left]] -= 1\n",
    "                if cnt[nums[left]] == 0:\n",
    "                    del cnt[nums[left]]\n",
    "                left += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import deque\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        diffs = set(nums)\n",
    "        res = 0\n",
    "        \n",
    "        for L in range(0, n):\n",
    "            sub_set = set()\n",
    "            for R in range(L, n):\n",
    "                sub_set.add(nums[R])\n",
    "\n",
    "                if sub_set == diffs:\n",
    "                    res += n - R\n",
    "                    break            \n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        # 双指针\n",
    "        n, m = len(nums), len(set(nums))\n",
    "        ans = 0\n",
    "        i = n\n",
    "        count = Counter()  # Counter(nums[i:j + 1])\n",
    "        for j in range(n - 1, -1, -1):\n",
    "            # 左移i，使得nums[i:j + 1]刚好为完全子数组\n",
    "            while i > 0 and len(count) < m:\n",
    "                i -= 1\n",
    "                count[nums[i]] += 1\n",
    "            \n",
    "            if len(count) == m:\n",
    "                ans += i + 1\n",
    "            \n",
    "            # 左移j\n",
    "            count[nums[j]] -= 1\n",
    "            if count[nums[j]] == 0:\n",
    "                count.pop(nums[j])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        m = len(set(nums))\n",
    "        cnt = Counter()\n",
    "        ret = left = 0\n",
    "        for x in nums:\n",
    "            cnt[x] += 1\n",
    "            while len(cnt) == m:\n",
    "                cnt[nums[left]] -= 1\n",
    "                if cnt[nums[left]] == 0:\n",
    "                    del cnt[nums[left]]\n",
    "                left += 1\n",
    "            ret += left\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        length = len(nums)\n",
    "        # 转换成集合\n",
    "        all_set = set(nums)\n",
    "        # 左右指针记录\n",
    "        left = 0\n",
    "        # 临时的set去存储\n",
    "        temp_set = set(nums[:1])\n",
    "        set_count = [0] * 2001\n",
    "        set_count[nums[0]] = 1\n",
    "        # count去记录完全子数字个数\n",
    "        count = 0 if temp_set != all_set else 1\n",
    "        # right直接往前走，检测到符合就记录\n",
    "        for right in range(1, len(nums)):\n",
    "            temp_set.add(nums[right])\n",
    "            set_count[nums[right]] += 1\n",
    "            if temp_set == all_set:\n",
    "                # 记录left移动的次数\n",
    "                while left <= right and set_count[nums[left]] > 1:\n",
    "                    count += length-right\n",
    "                    set_count[nums[left]] -= 1\n",
    "                    left += 1\n",
    "                if set_count[nums[left - 1]] == 0:\n",
    "                    temp_set.remove(nums[left-1])\n",
    "                else:\n",
    "                    count += 1\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "\n",
    "        # 暴力来一遍\n",
    "\n",
    "        num_sta = []\n",
    "\n",
    "        for ini in range(len(nums)):\n",
    "            if nums[ini] not in num_sta:\n",
    "                num_sta.append(nums[ini])\n",
    "        \n",
    "        ans = 0\n",
    "        for ini in range(len(nums)):\n",
    "            num_sta_tmp = []\n",
    "\n",
    "            for inj in range(ini, len(nums)):\n",
    "                if nums[inj] not in num_sta_tmp:\n",
    "                    num_sta_tmp.append(nums[inj])\n",
    "                \n",
    "                if len(num_sta_tmp) == len(num_sta):\n",
    "                    # print(ans, len(nums) - inj)\n",
    "                    ans += len(nums) - inj\n",
    "                    break\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        m = len(set(nums))\n",
    "        cnt = Counter()\n",
    "        ans = left = 0\n",
    "        for v in nums:  # 枚举子数组右端点 v=nums[i]\n",
    "            cnt[v] += 1\n",
    "            while len(cnt) == m:\n",
    "                x = nums[left]\n",
    "                cnt[x] -= 1\n",
    "                if cnt[x] == 0:\n",
    "                    del cnt[x]\n",
    "                left += 1\n",
    "            ans += left  # 子数组左端点 < left 的都是合法的\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        t = collections.Counter(nums)\n",
    "        ans = 0\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            counter = {}\n",
    "            counter[nums[i]] = 1\n",
    "            for j in range(i, n):\n",
    "                if nums[j] not in counter.keys():\n",
    "                    counter[nums[j]] = 0\n",
    "                counter[nums[j]] += 1\n",
    "                if len(counter) == len(t):\n",
    "                    ans += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        unique=[]\n",
    "        for x in nums:\n",
    "            if x not in unique:\n",
    "                unique.append(x)\n",
    "        def inornotin(u:List[int],n:List[int]):\n",
    "            for x in u:\n",
    "                if x in n:\n",
    "                    pass\n",
    "                else:\n",
    "                    return False\n",
    "            return True\n",
    "        ans=0\n",
    "        for i in range(len(nums)):\n",
    "            j=len(nums)\n",
    "            while i<j:\n",
    "                if inornotin(unique,nums[i:j]):\n",
    "                    ans=ans+1\n",
    "                    j=j-1\n",
    "                else:\n",
    "                    break\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\r\n",
    "        cnt = Counter()\r\n",
    "        n = len(set(nums))\r\n",
    "        ans = 0\r\n",
    "        l = 0\r\n",
    "        for r, x in enumerate(nums):\r\n",
    "            cnt[x] += 1\r\n",
    "            if len(cnt) == n:\r\n",
    "                while cnt[nums[l]] > 1:\r\n",
    "                    cnt[nums[l]] -= 1\r\n",
    "                    l += 1\r\n",
    "                ans += l + 1\r\n",
    "        return ans\r\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countCompleteSubarrays(self, nums: List[int]) -> int:\n",
    "        d = len(set(nums))\n",
    "\n",
    "        cnt = Counter()\n",
    "        res = 0\n",
    "        left = 0\n",
    "\n",
    "        for num in nums:\n",
    "            cnt[num] += 1\n",
    "            \n",
    "            while len(cnt)==d:\n",
    "                cnt[nums[left]] -= 1\n",
    "                if cnt[nums[left]]==0:\n",
    "                    del cnt[nums[left]]\n",
    "                left += 1\n",
    "\n",
    "            res += left\n",
    "\n",
    "        return res\n",
    "            \n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
